package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.ProductdetailsinfoDaoImpl;
import com.ysd.dao.impl.UserDaoImpl;
import com.ysd.entity.Productdetailsinfo;
import com.ysd.entity.User;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;
public class AdminLiuYanServlet extends BaseServlet {
	public void getliuyan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");//编码格式
		PrintWriter out = response.getWriter();//out对象
		
		UserDaoImpl uDao = new UserDaoImpl();
		String status = request.getParameter("status");
		int page = Integer.parseInt(request.getParameter("page"));
		int size = Integer.parseInt(request.getParameter("size"));
		List<User> list = uDao.getUserbypage(status, page, size);
		Gson gson = new Gson();
		String res  = gson.toJson(list);
		out.print(res);
		out.flush();
		out.close();
	}
	public void getliuyanBySize(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");//编码格式
		PrintWriter out = response.getWriter();//out对象
		
		UserDaoImpl uDao = new UserDaoImpl();
		String status = request.getParameter("status");
		List<User> list = uDao.getuser(status);
		Gson gson = new Gson();
		String res  = gson.toJson(list.size());
		out.print(res);
		out.flush();
		out.close();
	}
	public void updateById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");//编码格式
		PrintWriter out = response.getWriter();//out对象
		
		UserDaoImpl uDao = new UserDaoImpl();
		int id = Integer.parseInt(request.getParameter("id"));
		
		User user = new User(id, "已读",null);
		int n = uDao.updaMessage(user);
		if (n>0) {
			out.print(Result.toClient(true, "成功",""));
		} else {
			out.print(Result.toClient(false, "失败",""));
		}
		
		out.flush();
		out.close();
	}
	public void updateLiuYan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");//编码格式
		PrintWriter out = response.getWriter();//out对象
		
		UserDaoImpl uDao = new UserDaoImpl();
		int id = Integer.parseInt(request.getParameter("id"));
		String reply = request.getParameter("reply");
		User user = new User(id, null, reply);
		int n = uDao.updateMessage(user);
		if (n>0) {
			out.print(Result.toClient(true, "回复成功",""));
		} else {
			out.print(Result.toClient(false, "回复失败",""));
		}
		
		out.flush();
		out.close();
	}
	public void getById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");//编码格式
		PrintWriter out = response.getWriter();//out对象
		UserDaoImpl pcDao = new UserDaoImpl();
		int id = Integer.parseInt(request.getParameter("id"));
		User User = pcDao.getUserById(id);
		Gson gson = new Gson();
		String res = gson.toJson(User);
		out.print(res);
		
		out.flush();
		//清空
		out.close();
		//关闭
	}
	public void delliuyan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int cId = Integer.parseInt(request.getParameter("cid"));
		UserDaoImpl pcDao = new UserDaoImpl();//创建实现类对象
		int n= pcDao.delliuyan(cId);
		response.setCharacterEncoding("UTF-8");//编码格式
		PrintWriter out = response.getWriter();//out对象
		if (n>0) {
			out.print(Result.toClient(true, "删除成功",""));
		} else {
			out.print(Result.toClient(false, "删除失败",""));
		}
		out.flush();
		out.close();
	}

}
